#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <ctime>
//#include "piratename.h"
using namespace std;

bool load(vector<string>& firstNames,
		  vector<string>& middleNames,
		  vector<string>& lastNames)
{
// FIRST NAMES
    ifstream file;
    file.open("firstName.txt");

	if (file) {							// IF the file actually exists:
		while (file) {					// WHILE the file still has lines to read,
			string name; file >> name;	// load them
			firstNames.push_back(name);	// and store them in the vector
		}
		firstNames.pop_back();			// delete the blank one on the end (probably should fix this soon)
		file.close();					// close the file
	} else { return false; }			// IF IT DOES NOT EXIST, EXIT PROGRAM
	
// MIDDLE NAMES
    file.open("middleName.txt");

	if (file) {
		while (file) {
			string name; file >> name;
			middleNames.push_back(name);
		}
		middleNames.pop_back();
		file.close();
	} else { return false; }
	
// LAST NAMES
    file.open("lastName.txt");

	if (file) {
		while (file) {
			string name; file >> name;
			lastNames.push_back(name);
		}
		lastNames.pop_back();
		file.close();
	} else { return false; }
	
    return true;
}

// Does what it says it does: geenrates
string geenrate(vector<string>& firstNames,
			    vector<string>& middleNames,
				vector<string>& lastNames)
{
	stringstream ss;
	ss << firstNames.at(rand() % firstNames.size()) << " "
	   << middleNames.at(rand() % middleNames.size()) << " "
	   << lastNames.at(rand() % lastNames.size()) << endl;
	   
	   return ss.str();
}

// Instead of putting the names on the console screen,
// Put them inside a file called "output.txt"
void writeToFile(vector<string>& firstNames, vector<string>& middleNames, vector<string>& lastNames, unsigned long int geenrateNumber) {
	ofstream file; 
	file.open("output.txt");
	
	for (size_t i = 0; i < geenrateNumber; ++i) {
		file << geenrate(firstNames, middleNames, lastNames);
	}
}

int main(int argc, const char** argv) {
// handle command line arguments
	vector<string> args(argv, argv + argc);

// seeds the geenrator, since we want actual random names	
	srand((unsigned)time(NULL));
	
// check to see if proper cmd argument is supplied
	unsigned long int geenrateNumber;
	if (argc != 2) {
		cout << "Usage: a.exe [number]" << endl;
		return 1;
	} else {
		istringstream ss(args.at(1));
		ss >> geenrateNumber;
	}
	
	/** OLD **
	unsigned long int geenrateNumber;
	cout << "Enter number of times of geenration: ";
	cin >> geenrateNumber;
	 **     **/
	
    vector<string> firstNames;
    vector<string> middleNames;
    vector<string> lastNames;

	// crash protection
    if (!load(firstNames, middleNames, lastNames)) {
		cout << "Error:  one of the input files could not be found/read.\n";
		return 1;
	}
	
	writeToFile(firstNames, middleNames, lastNames, geenrateNumber);

    return 0;
}
